﻿var trunPageObj;//翻页组件对像
var fromEleIds = "#arapType,#tradeDate,#curTypeSipf,#oppPar,#mktCodeSipf,#arapBusiType,#arapAmt,#harapDate,#arapDate,#summary,#taCode,#arapDateNull";
var isModify;//是否修改
$(initPage);
function initPage() {
	//初始化翻页组件
    trunPageObj =  $(".pageNext").turnPage(10, exportList);
    $("td", "#rptA13List").click(function() {
		if($("#btnSave").attr("disabled") == false) {
			if (!confirm("当前编辑内容还未保存，是否继续？")) {
				return;
			}
		}
		
		$(".selectTD", "#rptA13List").removeClass("selectTD").find("img").attr("src","themes/comm/images/close.gif");
		$(this).addClass("selectTD").find("img").attr("src","themes/comm/images/open.gif");
		
		/*var arapType_text = $("span", this).text();
		$("#arapType").val($(this).attr("arapType"));
		$("#arapType_text").text(arapType_text.split("：")[1]);
		*/
		$("#arapReason").val($(this).attr("arapReason"));
		
//		$("#tradeDate").val($("#busiDate").val());
		
		$("#btnCancle").click();//重置状态

		trunPageObj.gotoPage(1);//搜索第一页
	});
	$("td", "#rptA13List").eq(1).click();//默认打开第一个
	
	$("#busiDate").change(function() {
		$(".selectTD", "#rptA13List").click();
	});
	
	//对方机构为TA代码时显示输入框
	$("#oppPar")[0].onchange = function() {
		if (this.value == "TA") {
			$("#taCode").show();
		} else {
			$("#taCode").hide();
		}
	};
	
	//选择应收应付类型，加载相应的应收付原因
	$("#arapType")[0].onchange = function() {
		arapTypeSelect(this);
	};
	
	$("#arapDateNull").click(function(){
		$("#arapDate").attr("readonly", $(this).attr("checked"));
	});
	
	
	//新增
	$("#btnAdd").click(function() {
		var newCompanyCode = $.trim($("#newCompanyCode").val());
		if(newCompanyCode == null || newCompanyCode == ""){
			alert("当前证券公司编号为空，不能进行新增");
			return;
		}
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		
		$("#arapDateNull").attr("checked",true);
		$("#arapDate").attr("disabled", true);
		
		isModify = false;
		clearValues();
	});
	
	//修改
	$("#btnEdit").click(function() {
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		if ($("#arapDateNull").attr("checked")) {
			$("#arapDate").attr("readonly",true);
		}
		isModify = true;
	});
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnEdit,#btnDelete,#btnSave,#btnCancle").attr("disabled", true);
		$("#btnAdd").attr("disabled", false);
		$(fromEleIds).attr("disabled", true);
		clearValues();
	});
	
	//保存
	$("#btnSave").click(function() {
		saveRptA13();
	});
	
	//删除
	$("#btnDelete").click(function() {
		delRptA13();
	});
	
	//查看历史
	$("#btnHist").click(function() {
		var url = BASE_PATH + "page/dataadjust/rpt_a13_hist.jsp?busiDate=" + $("#busiDate").val();
		window.showModalDialog(url,"","dialogWidth:800px;dialogHeight:480px;help:no;center:yes;status:no");
	});
	//调用存储过程重新生成A13数据
	$("#btnReGen").click(function() {
		$("#btnReGen").attr("disabled", true);//不允许再次操作
		$.post("m/submit_file/reGenA13", {
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert(data.msg.text);
				location.reload();
			} 
		}, "json");
	});
}

//清除表单值
function clearValues() {
	$(fromEleIds).each(function() {
		var d_val = $(this).attr("default-value");
		$(this).val(d_val ? d_val : "");
		
	});

	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

//保存
function saveRptA13() {
	if($.validInput("tradeDate", "交易日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("curTypeSipf", "币种", true)){
		return false;
	}
	if($.validInput("oppPar", "对方机构", true)){
		return false;
	}
	var oppPar = $.trim($("#oppPar").val());
	if (oppPar =="TA") {
		if($.validInput("taCode", "TA代码", true, "\<\>\'\"", 3)){
			return false;
		}
		oppPar = $.trim($("#taCode").val());
	}
	if($.validInput("mktCodeSipf", "市场代码", true)){
		return false;
	}
	if($.validInput("arapBusiType", "业务类型", true)){
		return false;
	}
	if($.validNumber("arapAmt", "应收应付金额", true, 999999999999999)){
		return false;
	}
	if($.validInput("harapDate", "已收付日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("arapDate", "应收付日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("summary", "摘要信息", false, "", 120)){
		return false;
	}
	
	var arapDate = "";
	if($("#arapDateNull").attr("checked") == false) {
		arapDate = $.trim($("#arapDate").val())
	}
	
	var action = isModify ? "modify" : "add";
	$.post("m/data_adjust/" + action, {
		    companyCode :$.trim($("#companyCode").val()),
			oldCompanyCode :$.trim($("#companyCode").val()),
			arapType :$.trim($("#arapType").val()),
			oldArapType :$.trim($("#oldArapType").val()),
			tradeDate:$.trim($("#tradeDate").val()),
			oldTradeDate:$.trim($("#oldTradeDate").val()),
			oppPar :oppPar,
			oldOppPar :$.trim($("#oldOppPar").val()),
			mktCodeSipf :$.trim($("#mktCodeSipf").val()),
			oldMktCodeSipf :$.trim($("#oldMktCodeSipf").val()),
			arapBusiType:$.trim($("#arapBusiType").val()),
			oldArapBusiType:$.trim($("#oldArapBusiType").val()),
			curTypeSipf:$.trim($("#curTypeSipf").val()),
			oldCurTypeSipf:$.trim($("#oldCurTypeSipf").val()),
			arapReason:$.trim($("#arapReason").val()),
			arapAmt:$.trim($("#arapAmt").val()),
			harapDate:$.trim($("#harapDate").val()),
			oldHarapDate:$.trim($("#oldHarapDate").val()),
			arapDate:arapDate,
			summary:$.trim($("#summary").val()),
			busiDate:$.trim($("#busiDate").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}

//删除
function delRptA13() {
		if (!confirm("确定要删除记录吗？")) {
		return;
	}
	$.post("m/data_adjust/delete", {
		    companyCode :$.trim($("#companyCode").val()),
			arapType :$.trim($("#arapType").val()),
			tradeDate:$.trim($("#oldTradeDate").val()),
			oppPar :$.trim($("#oldOppPar").val()),
			mktCodeSipf :$.trim($("#oldMktCodeSipf").val()),
			arapBusiType:$.trim($("#oldArapBusiType").val()),
			curTypeSipf:$.trim($("#oldCurTypeSipf").val()),
			harapDate:$.trim($("#oldHarapDate").val()),
			busiDate:$.trim($("#busiDate").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}

function queryList(){
	var pageno=trunPageObj.getCurrentPageNo();//当前第几页
	var pagecount=trunPageObj.getPageCount();//一页多少条
	var busiDate = $("#busiDate").val();
	$.post("m/data_adjust/query", {
		pageNo: pageno,
		pageSize : pagecount,
		busiDate : busiDate
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = [];
			if(data.rptA13Page && data.rptA13Page.dataList){
				var dataList = data.rptA13Page.dataList;
				$(dataList).each(function(i) {
					fileList.push("<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)'>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].id.arapType + "'>" +dataList[i].arapTypeText + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].arapReason + "'>" + dataList[i].arapReasonText + "</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatTradeDate + "</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].id.curTypeSipf + "'>" + dataList[i].curTypeSipfText + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].id.oppPar + "'>" + dataList[i].oppParText + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].id.mktCodeSipf + "'>" + dataList[i].mktCodeSipfText + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].id.arapBusiType + "'>" + dataList[i].arapBusiTypeText + "</td>");
					fileList.push("<td align='right'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].arapAmtText + "</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatHarapDate + "</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatArapDate + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap>" + (dataList[i].summary ? dataList[i].summary : "")  + "</td>");
					fileList.push("<td align='left'  height='25px' style='display:none;' class='queryList_linebg' nowrap value='" + dataList[i].id.companyCode + "'></td>");
					fileList.push("</tr>");
				});
			}
			$("#data_list").html(fileList.join(""));
			trunPageObj.setPageData(data.rptA13Page);
		} 
	}, "json");
}

function selectTR(obj) {
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	var tds = $(obj).find("td");
	$("#arapReason").val(tds.eq(1).attr("value"));
	$("#tradeDate,#oldTradeDate").val(tds.eq(2).text());
	$("#curTypeSipf,#oldCurTypeSipf").val(tds.eq(3).attr("value"));
	
	var oppPar = tds.eq(4).attr("value");
	var oppParText = tds.eq(4).text();
	if (oppParText.indexOf("TA代码:") == 0) {
		$("#oppPar").val("TA");
		$("#taCode,#oldOppPar").val(oppPar);
	} else {
		$("#oppPar,#oldOppPar").val(oppPar);
	}
	
	$("#mktCodeSipf,#oldMktCodeSipf").val(tds.eq(5).attr("value"));
	$("#arapBusiType,#oldArapBusiType").val(tds.eq(6).attr("value"));
	$("#arapAmt").val(tds.eq(7).text());
	$("#harapDate,#oldHarapDate").val(tds.eq(8).text());
	var arapDate = tds.eq(9).text();
	$("#arapDateNull").attr("checked",arapDate == "");
	$("#arapDate").val(arapDate);
	
	$("#summary").val(tds.eq(10).text());
	$("#companyCode").val(tds.eq(11).attr("value"));
	
	$("#arapType").val(tds.eq(0).attr("value"));
    $("#oldArapType").val(tds.eq(0).attr("value"));
	
	$("#btnSave,#btnCancle").attr("disabled", true);
	$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", false);
	$(fromEleIds).attr("disabled", true);
}

function changePage(url) {
	if($("#btnSave").attr("disabled") == false) {
		if (!confirm("当前编内容还未保存，是否继续？")) {
			return;
		}
	}
	location.href=BASE_PATH+url;
}

function arapTypeSelect(obj){
	var arapTypeValue = obj.options[obj.selectedIndex].label;
	$("#arapReason").attr("value",arapTypeValue);
}

function exportList() {
	var obj = window.showModalDialog(BASE_PATH + "page/export_select.jsp","","dialogWidth:200px;dialogHeight:80px;help:no;center:yes;status:no");
	if (obj) {
		window.open("m/data_adjust/exporta13?busiDate=" + $.trim($("#busiDate").val()) + "&expType=" + obj);
	}
}